<template>
  <!-- 关注用户 -->
  <div class="user-focus">
    <!-- 搜索用户 -->
    <div ref="userSearchRef" class="user-search">
      <a-affix :target="() => this.$refs.userSearchRef">
        <a-input-search
          placeholder="搜索用户"
          v-model="searchInputKey"
          :allowClear="true"
          @search="handleSerachUser"
        />
      </a-affix>
      <!-- 搜索用户项 -->
      <div
        v-for="(item, index) in searchUserList"
        :key="index"
        class="user-item"
      >
        <span>{{ item.nickName }}</span>
        <span>{{ item.userName }}</span>
        <a-button
          type="primary"
          size="small"
          @click="handleFocusUser(item.userName)"
          :disabled="item.userName == userInfo.userName"
          >关注用户</a-button
        >
      </div>
      <!-- 列表为空提示 -->
      <div v-show="searchUserList.length == 0" class="empty-list">暂无</div>
    </div>
    <!-- 关注列表 -->
    <div ref="focusListRef" class="focus-list">
      <a-affix :target="() => this.$refs.focusListRef">
        <div class="focus-list-title">已关注用户</div>
      </a-affix>
      <!-- 关注用户项 -->
      <div
        v-for="(item, index) in focusUserList"
        :key="index"
        class="user-item"
      >
        <span>{{ item.nickName }}</span>
        <span>{{ item.userName }}</span>
        <a-button
          type="default"
          size="small"
          @click="handleCancelFocus(item.userName)"
          >持续关注</a-button
        >
      </div>
      <!-- 列表为空提示 -->
      <div v-show="focusUserList.length == 0" class="empty-list">暂无</div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      searchInputKey: '',
      searchUserList: []
    }
  },
  computed: {
    ...mapState(['userInfo', 'focusUserList'])
  },
  methods: {
    handleSerachUser() {
      this.searchUserList = []
      if (this.searchInputKey.trim() !== '') {
        this.$api.user.getUserByUsername(this.searchInputKey).then((res) => {
          this.searchUserList.push(res.data)
        })
      }
    },
    handleFocusUser(userName) {
      this.$api.user
        .focusUser(userName)
        .then((res) => {
          this.$message.success('关注成功')
          this.$store.dispatch('setFocusUserListAsync')
        })
        .catch(() => {
          this.$message.info('您可能已经关注过TA了')
        })
    },
    handleCancelFocus(userName) {
      this.$message.success(userName + '表示很感谢您的关注 ♪(･ω･)ﾉ')
    }
  }
}
</script>
<style lang="less" scoped>
.user-focus {
  display: flex;
  height: 100%;
  .user-search {
    flex: 1;
    height: 100%;
    overflow: auto;
    border-right: 1px dashed #ccc;
    padding-right: 12px;
    .ant-input-search {
      box-shadow: 0 5px 10px -5px #666;
    }
  }
  .focus-list {
    flex: 1;
    height: 100%;
    overflow: auto;
    padding-left: 12px;
    .focus-list-title {
      height: 32px;
      line-height: 32px;
      background-color: #fff;
      box-shadow: 0 5px 10px -5px #666;
      border-radius: 4px;
      padding-left: 12px;
      user-select: none;
    }
  }
  //   列表中的用户项
  .user-item {
    height: 64px;
    line-height: 64px;
    border-bottom: 1px dotted #ccc;
    // text-align: right;
    min-width: 280px;
    display: flex;
    align-items: center;
    span {
      flex: 1;
    }
    .ant-btn {
      margin-right: 12px;
    }
  }
  //   列表为空提示
  .empty-list {
    font-size: 64px;
    font-weight: bold;
    text-align: center;
    padding-top: 48px;
    color: #ccc;
    min-width: 280px;
  }
}
</style>
